home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / cawf2st.zoo / expr.c < prev    next >
C/C++ Source or Header  |  1992-04-12  |  5KB  |  172 lines

  1. /*
  2.  *      expr.c - expression support functions for cawf(1)
  3.  */
  4.  
  5. /*
  6.  *      Copyright (c) 1991 Purdue University Research Foundation,
  7.  *      West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *      University Computing Center.  Not derived from licensed software;
  11.  *      derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *      Permission is granted to anyone to use this software for any
  14.  *      purpose on any computer system, and to alter it and redistribute
  15.  *      it freely, subject to the following restrictions:
  16.  *
  17.  *      1. The author is not responsible for any consequences of use of
  18.  *         this software, even if they arise from flaws in it.
  19.  *
  20.  *      2. The origin of this software must not be misrepresented, either
  21.  *         by explicit claim or by omission.  Credits must appear in the
  22.  *         documentation.
  23.  *
  24.  *      3. Altered versions must be plainly marked as such, and must not
  25.  *         be misrepresented as being the original software.  Credits must
  26.  *         appear in the documentation.
  27.  *
  28.  *      4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32.  
  33.  
  34. /*
  35.  * Asmcode(s, c) - assemble number/name code following backslash-character
  36.  *                 definition  - e. .g, "\\nPO"
  37.  */
  38.  
  39. char *
  40. Asmcode(s, c)
  41.         char **s;                       /* pointer to character after '\\' */
  42.         char *c;                        /* code destination (c[3]) */
  43. {
  44.         char *s1;
  45.  
  46.         s1 = *s + 1;
  47.         c[0] = c[1] = c[2] = '\0';
  48.         if ((c[0] = *s1) == '(') {
  49.                 s1++;
  50.                 if ((c[0] = *s1) != '\0') {
  51.                         s1++;
  52.                         c[1] = *s1;
  53.                 }
  54.         }
  55.         return(s1);
  56. }
  57.  
  58.  
  59. /*
  60.  * Delnum(nx) - delete number
  61.  */
  62.  
  63. Delnum(nx)
  64.         int nx;                         /* number index */
  65. {
  66.         char buf[MAXLINE];              /* message buffer */
  67.  
  68.         if (nx >= Nnr) {
  69.                 (void) sprintf(buf, " bad Delnum(%d) index", nx);
  70.                 Error(FATAL, LINE, buf, NULL);
  71.         }
  72.         while (nx < (Nnr - 1)) {
  73.                 Numb[nx] = Numb[nx + 1];
  74.                 nx++;
  75.         }
  76.         Nnr--;
  77. }
  78.  
  79.  
  80. /*
  81.  * Findnum(n, v, e) - find or optionally enter number value
  82.  */
  83.  
  84. Findnum(n, v, e)
  85.         char *n;                        /* register name */
  86.         int v;                          /* value */
  87.         int e;                          /* 0 = find, don't enter
  88.                                          * 1 = enter, don't find */
  89. {
  90.         int cmp, low, hi, mid;          /* binary search controls */
  91.         char c[3];                      /* name buffer */
  92.  
  93.         c[0] = n[0];
  94.         c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1];
  95.         c[2] = '\0';
  96.         low = mid = 0;
  97.         hi = Nnr - 1;
  98.         while (low <= hi) {
  99.                 mid = (low + hi) / 2;
  100.                 if ((cmp = strncmp(c, Numb[mid].nm, 2)) < 0)
  101.                         hi = mid - 1;
  102.                 else if (cmp > 0)
  103.                         low = mid + 1;
  104.                 else {
  105.                         if (e)
  106.                                 Numb[mid].val = v;
  107.                         return(mid);
  108.                 }
  109.         }
  110.         if ( ! e)
  111.                 return(-1);
  112.         if (Nnr >= MAXNR)
  113.                 Error(FATAL, LINE, " out of number registers at ", c);
  114.         if (Nnr) {
  115.                 if (cmp > 0)
  116.                         mid++;
  117.                 for (hi = Nnr - 1; hi >= mid; hi--)
  118.                         Numb[hi+1] = Numb[hi];
  119.         }
  120.         Nnr++;
  121.         Numb[mid].nm[0] = c[0];
  122.         Numb[mid].nm[1] = c[1];
  123.         Numb[mid].val = v;
  124.         return(mid);
  125. }
  126.  
  127.  
  128. /*
  129.  * Findparms(n) - find parameter registers
  130.  */
  131.  
  132. Findparms(n)
  133.         char *n;                        /* parameter name */
  134. {
  135.         char c[3];                      /* character buffer */
  136.         int i;                          /* temporary index */
  137.  
  138.         c[0] = n[0];
  139.         c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1];
  140.         c[2] = '\0';
  141.         for (i = 0; Parms[i].nm[0]; i++) {
  142.                 if (c[0] == Parms[i].nm[0] && c[1] == Parms[i].nm[1])
  143.                         return(i);
  144.         }
  145.         return(-1);
  146. }
  147.  
  148.  
  149. /*
  150.  * Findscale(n, v, e) - find and optionally enter scaling factor value
  151.  */
  152.  
  153. Findscale(n, v, e)
  154.         char n;                         /* scaling factor name */
  155.         double v;                       /* value */
  156.         int e;                          /* 0 = find, don't enter
  157.                                          * 1 = enter, don't find */
  158. {
  159.         int i;
  160.  
  161.         for (i = 0; Scale[i].nm; i++) {
  162.                 if (n == Scale[i].nm)
  163.                         break;
  164.         }
  165.         if (Scale[i].nm) {
  166.                 if (e)
  167.                         Scale[i].val = v;
  168.                 return(i);
  169.         }
  170.         return(-1);
  171. }
  172.